package leetcode.f1t100;

/**
 * 找出字符串数组的最长公共前缀
 * https://leetcode.com/problems/longest-common-prefix/
 * @Author lichongxiao
 * @Date 2022/9/28 11:24
 */
public class Q14_LongestCommonPrefix {
    public static String longestCommonPrefix(String[] strs) {
        int i = 0;
        StringBuilder sb = new StringBuilder();
        while(i>=0){
            char c= 0;
            for(String str : strs){
                if(i==str.length()){
                    i=-2;
                    break;
                }
                if(c == 0){
                    c = str.charAt(i);
                } else if(c != str.charAt(i)){
                    i=-2;
                    break;
                }
            }
            if(i>=0){
                sb.append(c);
            }
            i++;
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(longestCommonPrefix(new String[]{"flower","flow","flight"}));
    }
}
